//
//  DLMapViewController.m
//  DLSport
//
//  Created by DongL on 2017/4/21.
//  Copyright © 2017年 DongL. All rights reserved.
//

#import "DLMapViewController.h"
#import "DLSportPolyline.h"
#import "DLSportPolyline.h"

@interface DLMapViewController ()<MAMapViewDelegate>



@property (nonatomic, assign) BOOL hasSetStartAnno;

@end

@implementation DLMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:_mapView];
    
    [self setupMapView];
}

- (void)setupMapView {
    //隐藏标尺
    _mapView.showsScale = NO;
    //禁止旋转
    _mapView.rotateEnabled = NO;
    
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    _mapView.allowsBackgroundLocationUpdates = YES;
    _mapView.pausesLocationUpdatesAutomatically = NO;
    _mapView.delegate = self;
}

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    
    if (updatingLocation == NO) {
        return;
    }
    
    //更新位置后,设置地图中心点
    _mapView.centerCoordinate = userLocation.location.coordinate;
    
    
    //添加起点大头针
    if (_hasSetStartAnno == NO && self.track.startAnno) {
        
        [_mapView addAnnotation:self.track.startAnno];
        _hasSetStartAnno = YES;
    }
    
    
    
    //生成新的折线  每段起点都是上一段的终点
    DLSportPolyline *newPolyline = [self.track appendPolylineWithDest:userLocation.location];
    
    _totalTime += newPolyline.time;
    
    _totalDistance += newPolyline.distance;
    
    _avgSpeed = newPolyline.speed;
    //添加折线
    [_mapView addOverlay:newPolyline];
   [[NSNotificationCenter defaultCenter] postNotificationName:@"DLSportLocationDidUpdateNote" object:nil userInfo:nil];
}

-(void)setMapType:(DLMapType)mapType {
    _mapType = mapType;
    
    _mapView.mapType = (MAMapType)_mapType;
}

//设置大头针视图时调用
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        //设置图片
        annotationView.image = [UIImage imageNamed:self.track.sportTypeImgName];
        //设置中心点偏移，使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -annotationView.image.size.height * 0.5);
        return annotationView;
    }
    return nil;
}

//设置覆盖物样式
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[DLSportPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        //线宽
        polylineRenderer.lineWidth    = 4.f;
        
        //描边颜色 根据折线的速度来改变
        DLSportPolyline *polyline = overlay;
        
        polylineRenderer.strokeColor  = polyline.storkeColor;
        //拐点样式
        polylineRenderer.lineJoinType = kMALineJoinRound;
        //头尾样式
        polylineRenderer.lineCapType  = kMALineCapRound;
        
        return polylineRenderer;
    }
    
    return nil;
}

@end
